Imports System
Imports System.Data
Imports System.Math
Imports System.Type

Namespace xpComponent.Math
    Public NotInheritable Class Mathematics
#Region "Function add two arguiment             "
        Public Overloads Shared Function Add(ByVal Arg1 As Object, ByVal Arg2 As Object) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Convert.ToDouble(Arg1) + Convert.ToDouble(Arg2)
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Add(ByVal Arg1 As String, ByVal Arg2 As String) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Convert.ToDouble(Arg1) + Convert.ToDouble(Arg2)
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Add(ByVal Arg1 As Double, ByVal Arg2 As Double) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Arg1 + Arg2
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Add(ByVal Arg1 As String, ByVal Arg2 As Double) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Convert.ToDouble(Arg1) + Arg2
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Add(ByVal Arg1 As Double, ByVal Arg2 As String) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Arg1 + Convert.ToDouble(Arg2)
                End If
            End If
            Return -1
        End Function
#End Region

#Region "Function substract two arguiment       "
        Public Overloads Shared Function Substract(ByVal Arg1 As Object, ByVal Arg2 As Object) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Convert.ToDouble(Arg1) - Convert.ToDouble(Arg2)
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Substract(ByVal Arg1 As String, ByVal Arg2 As String) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Convert.ToDouble(Arg1) - Convert.ToDouble(Arg2)
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Substract(ByVal Arg1 As Double, ByVal Arg2 As Double) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Arg1 - Arg2
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Substract(ByVal Arg1 As String, ByVal Arg2 As Double) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Convert.ToDouble(Arg1) - Arg2
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Substract(ByVal Arg1 As Double, ByVal Arg2 As String) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Arg1 - Convert.ToDouble(Arg2)
                End If
            End If
            Return -1
        End Function
#End Region

#Region "Function multiply two arguiment        "
        Public Overloads Shared Function Multiply(ByVal Arg1 As Object, ByVal Arg2 As Object) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Convert.ToDouble(Arg1) * Convert.ToDouble(Arg2)
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Multiply(ByVal Arg1 As String, ByVal Arg2 As String) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Convert.ToDouble(Arg1) * Convert.ToDouble(Arg2)
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Multiply(ByVal Arg1 As Double, ByVal Arg2 As Double) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Arg1 * Arg2
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Multiply(ByVal Arg1 As String, ByVal Arg2 As Double) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Convert.ToDouble(Arg1) * Arg2
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Multiply(ByVal Arg1 As Double, ByVal Arg2 As String) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    Return Arg1 * Convert.ToDouble(Arg2)
                End If
            End If
            Return -1
        End Function
#End Region

#Region "Function divide two arguiment          "
        Public Overloads Shared Function Divide(ByVal Arg1 As Object, ByVal Arg2 As Object) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    If Convert.ToDouble(Arg1) = 0 Then Return 0
                    If Convert.ToDouble(Arg2) = 0 Then Return -1
                    Return Convert.ToDouble(Arg1) / Convert.ToDouble(Arg2)
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Divide(ByVal Arg1 As String, ByVal Arg2 As String) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    If Convert.ToDouble(Arg1) = 0 Then Return 0
                    If Convert.ToDouble(Arg2) = 0 Then Return -1
                    Return Convert.ToDouble(Arg1) / Convert.ToDouble(Arg2)
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Divide(ByVal Arg1 As Double, ByVal Arg2 As Double) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    If Convert.ToDouble(Arg1) = 0 Then Return 0
                    If Convert.ToDouble(Arg2) = 0 Then Return -1
                    Return Arg1 / Arg2
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Divide(ByVal Arg1 As String, ByVal Arg2 As Double) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    If Convert.ToDouble(Arg1) = 0 Then Return 0
                    If Convert.ToDouble(Arg2) = 0 Then Return -1
                    Return Convert.ToDouble(Arg1) / Arg2
                End If
            End If
            Return -1
        End Function

        Public Overloads Shared Function Divide(ByVal Arg1 As Double, ByVal Arg2 As String) As Double
            If IsNumeric(Arg1) Then
                If IsNumeric(Arg2) Then
                    If Convert.ToDouble(Arg1) = 0 Then Return 0
                    If Convert.ToDouble(Arg2) = 0 Then Return -1
                    Return Arg1 / Convert.ToDouble(Arg2)
                End If
            End If
            Return -1
        End Function
#End Region

#Region "Function factorial                     "
        Public Overloads Shared Function Factorial(ByVal Arg As Integer) As Integer
            If Arg.ToString >= "1000" Then Return -1
            If Arg < 0 Then Return -1
            If Arg = 0 Then
                Return 1
            Else
                Return Arg + Factorial(Arg - 1)
            End If
        End Function

        Public Overloads Shared Function Factorial(ByVal Arg As Long) As Long
            If Arg.ToString >= "1000" Then Return -1
            If Arg < 0 Then Return -1
            If Arg = 0 Then
                Return 1
            Else
                Return Arg + Factorial(Arg - 1)
            End If
        End Function

        Public Overloads Shared Function Factorial(ByVal Arg As String) As Integer
            If Arg.ToString >= "1000" Then Return -1
            If IsNumeric(Arg) Then
                Dim tmp As Integer = CInt(Arg)
                If tmp < 0 Then Return -1
                If tmp = 0 Then
                    Return 1
                Else
                    Return tmp + Factorial(tmp - 1)
                End If
            End If
        End Function

        Public Overloads Shared Function Factorial(ByVal Arg As Object) As Integer
            If Arg.ToString >= "1000" Then Return -1
            If IsNumeric(Arg) Then
                Dim tmp As Integer = CInt(Arg)
                If tmp < 0 Then Return -1
                If tmp = 0 Then
                    Return 1
                Else
                    Return tmp + Factorial(tmp - 1)
                End If
            End If
        End Function

        Public Overloads Shared Function Factorial(ByVal Arg As Integer, ByVal Rang As Integer) As Integer
            If Arg.ToString > Rang Then Return -1
            If Arg < 0 Then Return -1
            If Arg = 0 Then
                Return 1
            Else
                Return Arg + Factorial(Arg - 1)
            End If
        End Function

        Public Overloads Shared Function Factorial(ByVal Arg As Long, ByVal Rang As Integer) As Long
            If Arg.ToString > Rang Then Return -1
            If Arg < 0 Then Return -1
            If Arg = 0 Then
                Return 1
            Else
                Return Arg + Factorial(Arg - 1)
            End If
        End Function

        Public Overloads Shared Function Factorial(ByVal Arg As String, ByVal Rang As Integer) As Integer
            If Arg.ToString > Rang Then Return -1
            If IsNumeric(Arg) Then
                Dim tmp As Integer = CInt(Arg)
                If tmp < 0 Then Return -1
                If tmp = 0 Then
                    Return 1
                Else
                    Return tmp + Factorial(tmp - 1)
                End If
            End If
        End Function

        Public Overloads Shared Function Factorial(ByVal Arg As Object, ByVal Rang As Integer) As Integer
            If Arg.ToString > Rang Then Return -1
            If IsNumeric(Arg) Then
                Dim tmp As Integer = CInt(Arg)
                If tmp < 0 Then Return -1
                If tmp = 0 Then
                    Return 1
                Else
                    Return tmp + Factorial(tmp - 1)
                End If
            End If
        End Function
#End Region

#Region "Function average and Tax               "
        Public Overloads Function Average(ByVal Arg() As Integer) As Double
            Dim count As Integer = 0
            Dim values As Double = 0
            Dim tmp() As Integer = Arg
            Dim lent As Integer = tmp.Length
            While count <= lent - 1
                values = values + Convert.ToDouble(tmp.GetValue(count))
                count = count + 1
            End While
            If values = 0 Then Return 0
            If values > 0 Then Return values / lent
            Return -1
        End Function

        Public Overloads Function Tax(ByVal Arg As Double) As Double
            If Arg > 0 Then
                Return Arg + (Arg * (7 / 100))
            End If
            Return -1
        End Function

        Public Overloads Function Tax(ByVal Arg As Double, ByVal percent As Double) As Double
            If percent > 0 And percent < 100 Then
                If Arg > 0 Then
                    Return Arg + (Arg * (percent / 100))
                End If
            End If
            Return -1
        End Function
#End Region
    End Class
End Namespace